package com.gm.service.user;

import com.github.miemiedev.mybatis.paginator.domain.PageBounds;
import com.gm.common.Pager;
import com.gm.dao.role.IRoleDao;
import com.gm.model.po.Role;
import com.gm.tools.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
/**
 * Created by Administrator on 2017/6/13.
 */
@Service
public class RoleSerivce {
    @Autowired
    private IRoleDao roleDao;

        public List getALlRoleList(Pager pager){
            PageBounds pageBounds = new PageBounds(pager.getCurrentPage(), pager.getPageSize() );
            return roleDao.getRoleListPage(pageBounds);
        }

    public List getALlRole(){
        return roleDao.getRoleAll();
    }

    public int addRole(String roleName,Integer rightId[]){
        Role role = new Role();
        role.setName(roleName);
        role.setCreateTime(DateUtils.getCurrentUnixTime());
        role.setUpdateTime(DateUtils.getCurrentUnixTime());
        String str = "";
        for (Integer i : rightId) {
            str += i+",";
        }
        str = str.substring(0,str.length()-1);
        role.setRights(str);
        int ret =roleDao.insert(role);
        return ret;
    }

    public  Role selectByPrimaryKey(Integer id){
        return roleDao.selectByPrimaryKey(id);
    }


    public int updateRole(Role role,Integer rightId[]){
        Role r = roleDao.selectByPrimaryKey(role.getId());
        r.setName(role.getName());
        r.setUpdateTime(DateUtils.getCurrentUnixTime());
        String str = "";
        for (Integer i : rightId) {
            str += i+",";
        }
        str = str.substring(0,str.length()-1);
        r.setRights(str);
        int ret =roleDao.updateByPrimaryKey(r);
        return ret;
    }

    public int  deleteRole(Integer roleId){
        return roleDao.deleteByPrimaryKey(roleId);
    }
}
